Use XNN fused RoPE for HF-style RoPE patterns#18627
Draft
GregoryComer wants to merge 1 commit intopytorch:mainfrom
Draft
Use XNN fused RoPE for HF-style RoPE patterns#18627GregoryComer wants to merge 1 commit intopytorch:mainfrom
GregoryComer wants to merge 1 commit intopytorch:mainfrom
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18627
Note: Links to docs will display an error until the docs builds have been completed. ❌ 2 New Failures, 2 Unrelated FailuresAs of commit 6478082 with merge base 19bbeac ( NEW FAILURES - The following jobs have failed:
FLAKY - The following jobs failed but were likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
4f9fc5c to
da422b8
Compare
154321b to
f4fbeb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pattern match HF style RoPE and delegate to XNNPACK. Relax slice constraints and add unsqueeze, as well, since XNNPACK supports them and it's easiest to not have to conditionally partition them for RoPE only when fusable.
This gives 1-6% performance uplift across decode and prefill for LLMs when fused (HF models, primarily).
Matched pattern:
See modeling_qwen3.py for an example. I checked 18 LLMs from transformers and about 3/4 use the above pattern.
XNNPACK's RoPE op logically does the following:
If the pattern isn't matched, we'll just continue to run decomposed. In order to use the XNNPACK kernel, we do need to combine the cos and sin weights into a single weight tensor. This requires us to modify the weights. Note that the weights depend on position IDs, so this does end up in the graph.
The re-written
apply_rotary_pos_embimpl looks like this (post fusion pass). Note that we're assuming that sin and cos second half is duplicated first half. The fusion pass only fuses when it sees that these tensors came from a cat([x, x]) op. We'll conservatively not fuse if this isn't the case. I've tested various HF models and found that it does fuse for the common pattern that most models use.In theory, we could do even more aggressive re-writing to fuse even more, but this seems sufficient for now.
cc @digantdesai @cbilgin